import re

dataList = []


def getVerifyStr(dictp, str):
	if str not in dictp:
		print("!!! not " + str + " params regex=''")
		return ""

	return dictp[str]


def getVerifyRegex(dictp):
	a = getVerifyStr(dictp, "regex")
	if a == "":
		a = getVerifyStr(dictp, "grep")
	if a == "":
		a = getVerifyStr(dictp, "grex")

	return a


def getVerifyNO(dictp, noname):
	if noname not in dictp:
		print("error: not exist " + noname + " params")
		return -1

	no = verifyNO(dictp[noname])
	return no


def verifyNO(params):
	nostr = params.strip(' ')
	try:
		no = int(nostr)

	except ValueError:
		print("input error no. no = " + nostr)
		return -1

	if no >= len(dataList):
		print("input error no,data max len=%d" % (len(dataList)))
		return -1

	return no


def regexFunc(content, rawregstr):
	regstr = rawregstr.strip(" ")
	# print("regex is :" + regstr[1:-1])

	try:
		reg = re.compile(regstr[1:-1])

		# print("scan contnt:",content)
		# print("regex:",regstr)

		match = reg.search(content)

		if match == None:
			return (False, None)
		else:
			return (True, match)
	except:
		# print("regex function is error")
		return (False, None)


def writeList(orgdata, filename):
	fs = None

	try:
		fs = open(filename, 'w', encoding='gbk')

		for key in orgdata:
			v = orgdata[key]
			formas = "{}:{}".format(key, v)
			fs.write(formas)

	except OSError as reason:
		print('出错啦！' + str(reason))

	finally:
		if fs != None:  # in locals():
			fs.close()

	"""
	with open(filename,'w',encoding='gbk') as fs:
		for key in orgdata:
			v = orgdata[key]
			formas = "{}:{}".format(key,v)
			fs.write(formas)
	 """


def getContent(filename):
	global dataList

	rawContent = {"title": filename + "\n", "content": {}}

	file = filename.strip(' ')
	print("get filename:", file)
	# file_object = open(file,'r',encoding='utf-8')

	lineno = 0
	file_object = None
	try:
		file_object = open(file, 'r', encoding='utf-8')

		for line in file_object:
			rawContent["content"][lineno] = line
			lineno = lineno + 1

		dataList = []
		dataList.append(rawContent)

	except OSError as reason:
		print('出错啦！' + str(reason))

	finally:
		if file_object != None:  # in locals():
			file_object.close()

	print("create list lineno =%d  total number=%d " % (lineno, len(rawContent["content"])))


def dispList():
	for index, dictp in enumerate(dataList):
		title = dictp["title"]
		content = dictp["content"]

		print("********  no %d  *********\n  total item = %d   \n    title:  \n %s******* end *******" % (
			index, len(content), title))


def dispContent(params):
	nostr = params.strip(' ')

	if nostr == "all":
		dispList()
		return

	no = verifyNO(nostr)
	if no == -1:
		return;

	title = dataList[no]["title"];
	print("******** no %d **********\n    itemnumber=%d\n     title :      \n %s *************" % (
		no, len(dataList[no]["content"]), title))

	number = 0
	for key, value in dataList[no]["content"].items():
		number = number + 1
		print("NO=" + str(number) + " key:" + str(key) + " con:" + value.strip("\n"))


def findContent(orgdict, constr):
	for key, value in orgdict.items():
		if value == constr:
			return (True, key)
	return (False, key)


# jf fie filename
def joinContent(filename):
	global dataList

	equalContent = {"title": "equal " + filename + "\n", "content": {}}

	orgdata = dataList[0]["content"]

	file = filename.strip(' ')
	print("get filename:", file)

	k = [key for key in dataList[0]["content"]]
	lineno = max(k) + 1

	dataList[0]["title"] = dataList[0]["title"] + file + "\n"

	file_object = None
	addnumber = 0
	try:
		code = input("\nInput(utf-8  gbk):>>")
		code = code.strip("\n").strip(" ")
		if code == "":
			code = "utf-8"

		file_object = open(file, 'r', encoding=code)

		for line in file_object:

			(b, key) = findContent(orgdata, line)
			if b == False:
				orgdata[lineno] = line;
				lineno = lineno + 1
				addnumber = addnumber + 1
			else:
				equalContent["content"][key] = line


	except OSError as reason:
		print('出错啦！' + str(reason))

	finally:
		if file_object != None:  # in locals():
			file_object.close()

	if len(equalContent["content"]) != 0:
		dataList.append(equalContent)

	print("add number =%d  total number=%d  equallist=%d" % (addnumber, len(orgdata), len(equalContent["content"])))


def getKParams(str):
	params = {}  # {"sno":"0","dno":"1","regex"=" "}

	reg = r'(\s*\w+\s*=\s*\w+\s*)|(\s*\w+\s*=\s*[\'\"][^\'\"]\s*\w+\s*[\'\"]\s*)'
	comlist = re.split(reg, str)

	for unit in comlist:

		# print(unit)
		if unit == None:
			continue

		p = unit.split("=")
		if len(p) <= 1:
			continue

		# print(p)
		params[p[0].strip(" ")] = p[1].strip(" ")

	return params


# createlist sno=1 regex="this is ok"
def createList(params):
	global dataList
	newList = {}

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	regexstr = getVerifyRegex(dictp)

	print("sno=" + str(sno) + "  regex=" + regexstr)

	orgdata = dataList[sno]

	title = orgdata["title"]
	newList["title"] = title + regexstr + "\n"
	newList["content"] = {}

	for key, value in orgdata["content"].items():
		(b, match) = regexFunc(value, regexstr)
		if b == True:
			newList["content"][key] = value

	print("create new list item =" + str(len(newList["content"])))

	if len(newList["content"]) != 0:
		dataList.append(newList)


# joinlist sno=1 dno=2 regex="this is ok"
def joinList(params):
	global dataList
	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	dno = getVerifyNO(dictp, "dno")
	if dno == -1:
		return;

	regexstr = getVerifyRegex(dictp)

	print("sno=" + str(sno) + "  regex=" + regexstr + " dno=" + str(dno))

	orgdata = dataList[sno]
	desdata = dataList[dno]

	title = desdata["title"]
	desdata["title"] = desdata["title"] + title + regexstr + "\n"

	(matchnumber, newnumber) = (0, 0)
	for key, value in orgdata["content"].items():

		(b, match) = regexFunc(value, regexstr)
		if b == True:
			matchnumber = matchnumber + 1

			if key not in desdata["content"]:
				newnumber = newnumber + 1
				# print("match is :" + value + "regex:" + regexstr)
				desdata["content"][key] = value

	print("join new deslist totalnum =" + str(len(desdata["content"])))
	print("match num=%d  new num = %d \n" % (matchnumber, newnumber))


# dataList.append(newList)


# getc sno regex
def getChar(params):
	global dataList

	dictp = getKParams(params.strip(' '))
	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	rawstr = getVerifyRegex(dictp)
	print(" sno=" + str(sno) + "  regex=" + rawstr)
	raw = rawstr.strip(" ")

	len = getVerifyStr(dictp, "len")

	if len == "":
		len = 2
	else:
		len = int(len.strip(" "))

	regexstr = '".{0,' + str(len) + '}' + raw[1:-1] + '.{0,' + str(len) + '}"'
	print(" sno=" + str(sno) + "  regex=" + regexstr)

	orgdata = dataList[sno]
	charSet = {}
	matchcontent = 0;
	for key, value in orgdata["content"].items():

		(b, match) = regexFunc(value, regexstr)
		if b == True:
			matchcontent = matchcontent + 1
			mstr = match.group(0)
			# print("match mstr:" + mstr)  # + " group(1):" + group(1))
			if mstr in charSet:
				charSet[mstr] = charSet[mstr] + 1
			else:
				charSet[mstr] = 1

	number = 0
	for key, value in charSet.items():
		number = number + 1
		print("NO=" + str(number) + " key:" + key + " number:" + str(value))

	print("match content:" + str(matchcontent))


# filter sno = 1 regex= "this is ok"   
def filterList(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	regexstr = getVerifyRegex(dictp)

	print(" sno=" + str(sno) + "  regex=" + regexstr)

	orgdata = dataList[sno]
	prenumber = len(orgdata["content"])

	orgdata["title"] = orgdata["title"] + " del :" + regexstr + "\n"

	newList = {}
	newList["content"] = {}

	for key, value in orgdata["content"].items():
		(b, match) = regexFunc(value, regexstr)
		if b == True:
			newList["content"][key] = value

	print("filter list total:" + str(len(newList["content"])))

	for key in newList["content"].keys():
		if key in orgdata["content"]:
			del orgdata["content"][key]

	print("list is post=%d  now item is =%d" % (prenumber, len(orgdata["content"])))


# dataList.append(newList)


def dropList(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	dno = getVerifyNO(dictp, "dno")
	if dno == -1:
		return;

	regexstr = getVerifyRegex(dictp)

	orgdata = dataList[sno]
	desdata = dataList[dno]

	title = orgdata["title"]
	desdata["title"] = desdata["title"] + " **** del ******\n" + title

	(matchnumber, newnumber) = (0, 0)
	for key, value in orgdata["content"].items():

		(b, match) = regexFunc(value, regexstr)
		if b == True:
			matchnumber = matchnumber + 1

			if key in desdata["content"]:
				newnumber = newnumber + 1
				del desdata["content"][key]

	print("desdata item = " + str(len(desdata["content"])))
	print("match num=%d  del num = %d \n" % (matchnumber, newnumber))


# dataList.append(newList)


def diffList(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	dno = getVerifyNO(dictp, "dno")
	if dno == -1:
		return;

	orgdata = dataList[sno]
	desdata = dataList[dno]

	newList = {}
	newList["content"] = {}
	newList["title"] = "diff sno=" + str(sno) + " dno=" + str(dno) + "\n"

	notmatch = 0
	for key, value in orgdata["content"].items():
		if key not in desdata["content"]:
			notmatch = notmatch + 1
			newList["content"][key] = value

	for key, value in desdata["content"].items():
		if key not in orgdata["content"]:
			notmatch = notmatch + 1
			newList["content"][key] = value

	print("create a diff list. difflist item =" + str(notmatch))

	if notmatch != 0:
		dataList.append(newList)


# eq sno dno
def equalList(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	dno = getVerifyNO(dictp, "dno")
	if dno == -1:
		return;

	orgdata = dataList[sno]
	desdata = dataList[dno]

	newList = {}
	newList["content"] = {}
	newList["title"] = "equal sno=" + str(sno) + " dno=" + str(dno) + "\n"

	match = 0
	for key, value in orgdata["content"].items():
		if key in desdata["content"]:
			match = match + 1
			newList["content"][key] = value

	print("create a equal list. equallist item =" + str(match))

	if match != 0:
		dataList.append(newList)


# del no
def delList(params):
	global dataList

	no = verifyNO(params)
	if no == -1:
		return;

	title = dataList[no]["title"];
	print("********************\n    itemnumber=%d     title :      \n %s **************" % (
		len(dataList[no]["content"]), title))

	del dataList[no]


# save sno= xx  name=“this "
def saveList(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	sno = getVerifyNO(dictp, "sno")
	if sno == -1:
		return;

	if "name" not in dictp:
		print("error: name param no exist ")
		return -1

	filename = dictp["name"][1:-1]
	print(" sno=%d name=%s" % (sno, filename))

	orgdata = dataList[sno]["content"]

	writeList(orgdata, filename)


# delkey sno=0 key=
def delKey(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	no = getVerifyNO(dictp, "sno")
	if no == -1:
		return;

	if "key" not in dictp:
		print("error: key param no exist ")
		return -1

	skeystr = dictp["key"]
	try:
		skey = int(skeystr)

	except ValueError:
		print("input error key = " + skeystr)
		return -1

	print("no =%d  key=%d" % (no, skey))

	if skey in dataList[no]["content"]:
		cont = dataList[no]["content"][skey];
		del dataList[no]["content"][skey];

		print("key:%d: cont:%s" % (skey, cont.strip()))


# fk sno=1 skey=xxx dkey=yyy
def findKey(params):
	global dataList

	dictp = getKParams(params.strip(' '))

	no = getVerifyNO(dictp, "sno")
	if no == -1:
		return;

	if "skey" not in dictp:
		print("error: skey param no exist ")
		return -1

	skeystr = dictp["skey"]
	try:
		skey = int(skeystr)

	except ValueError:
		print("input error skey = " + skeystr)
		return -1

	if "dkey" not in dictp:
		print("error: dkey param no exist ")
		dkeystr = "-1"  # dictp["skey"]
	else:
		dkeystr = dictp["dkey"]

	try:
		dkey = int(dkeystr)

	except ValueError:
		print("input error dkey = " + dkeystr)
		return -1

	if dkey == -1:
		k = [key for key in dataList[0]["content"]]
		dkey = max(k) + 1

	print("no =%d  skey=%d  dkey=%d" % (no, skey, dkey))

	if skey > dkey:
		print("skey must less dkey")
		return

	for i in range(skey, dkey + 1):
		if i in dataList[no]["content"]:
			cont = dataList[no]["content"][i];
			print("key:%d: cont:%s" % (i, cont.strip()))


def getContentWithKey(filename):
	global dataList

	rawContent = {"title": filename + "\n", "content": {}}

	file = filename.strip(' ')
	print("get filename:", file)

	file_object = None
	try:
		file_object = open(file, 'r', encoding='gbk')

		for unit in file_object:
			ls = unit.split(":", 1)
			# print(ls)

			if len(ls) < 2:
				print("file format is error")
				return

			lineno = int(ls[0].strip(' '))
			line = ls[1]

			rawContent["content"][lineno] = line

	except OSError as reason:
		print('出错啦！' + str(reason))

	finally:
		if file_object != None:  # in locals():
			file_object.close()

	print("read total number=%d " % (len(rawContent["content"])))

	if len(rawContent["content"]) != 0:
		dataList.append(rawContent)


systemCom = {
	"cf": getContent,
	"jf": joinContent,
	"cl": createList,
	"jl": joinList,
	"filter": filterList,
	"list": dispContent,
	"del": delList,
	"drop": dropList,
	"key": findKey,
	"save": saveList,
	"diff": diffList,
	"equal": equalList,
	"getf": getContentWithKey,
	"getc": getChar,
	"delkey": delKey
}

"""
(1)（createlist  filename）
（2）       读取文件，加入到现在的一个list中（join）。（join sourelist  filename）
 
（3）       创建一个list，根据现有的list，过滤新的数据到该list。（createlist，sourcelist，r=““）。
（4）        加入新的时间到该list。（join sourcelist =， regex，  des）。
（5）       过滤现有的list（filter sourcelist r）。
 
（6）       显示全部的list，并显示正则（list， list）
（7）       显示某一个list的数据（list sourcelist : disp 0）
"""

if __name__ == '__main__':

	while True:

		line = input("\nInput:>>")

		line.strip('\n')
		# print(line)

		if line == '':
			continue

		# line.strip('\n')

		commands = line.split(' ', 1)

		if len(commands) != 2:
			print("input error command.command list ;\n")
			for key in systemCom.keys():
				print(key)
			continue

		if commands[0] in systemCom.keys():
			systemCom[commands[0]](commands[1])
		else:
			print("error command")
			for key in systemCom.keys():
				print(key)

"""    
#    rawDict = getContent("e:/AI数据/陈述.csv")
# 
input:cf e:/ai数据/陈述.csv
get filename: e:/ai数据/陈述.csv
input:cl sno=0 regex="在开会"
sno=0  regex="在开会"
create new list item =16
input:jl sno=0 dno=1 regex="在忙"

(^|[^不])是不是


cf e:/AI数据和新方标注/新方陈述.csv     6268
jf e:/AI数据和新方标注/新方询问.csv     4390
                          10658

cl sno=0 regex="(^|[^不])是不是"
diff sno=1 dno=2

save sno=1 name="e:/new/是不是.csv"
save sno=2 name="e:/new/有/没有.csv"

getf e:/new/是不是.csv
getc sno=2 regex=".{2}不是.{2}"

"""
